package cn.magic.org.service.impl;

import cn.magic.basic.service.impl.BaseServiceImpl;
import cn.magic.basic.utils.MD5Utils;
import cn.magic.basic.utils.PageList;
import cn.magic.basic.utils.StrUtils;
import cn.magic.org.domain.Employee;
import cn.magic.org.mapper.EmployeeMapper;
import cn.magic.org.query.EmployeeQuery;
import cn.magic.org.service.IEmployeeService;
import cn.magic.user.domain.Logininfo;
import cn.magic.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
* @Description: 部门业务层实现类
* @Author: Mr.pc
* @Version: 1.0
* @Date:  2022/6/21 18:03
*/
@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;


    @Override
    @Transactional
    public void save(Employee employee) {
        // 1.初始化emplpyee，并保存，这是引用传递
        initEmployee(employee);
        employeeMapper.save(employee);
        // 2.初始化logininfo，并保存
        Logininfo logininfo = employee2Logininfo(employee);
        logininfoMapper.save(logininfo);
        // 3.将logininfo的主键Id设置到employee的外键，并修改
        employee.setLogininfoId(logininfo.getId());
        employeeMapper.update(employee);
        // 初始化角色关联表数据
        initRelationTable(employee);

    }

    /**
    * @Title: initRelationTable
    * @Description: 初始化角色关联表数据
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/13 15:24
    * @Parameters: [employee]
    * @Return void
    */
    private void initRelationTable(Employee employee) {
        // 2.根据roleId和菜单Id和权限Id进行新增
        if (employee.getRoles() != null){
            // 新增员工对应的角色关联表的所有数据
            employeeMapper.saveEmployeeRole(employee.getId(),employee.getRoles());
        }


    }

    /** 
    * @Title: employee2Logininfo
    * @Description: 将employee的数据拷贝到logininfo中
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/6 21:35  
    * @Parameters: [employee]
    * @Return cn.magic.user.domain.Logininfo
    */
    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        return logininfo;
    }

    /** 
    * @Title: initEmployee
    * @Description: 初始化employee
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/6 21:35  
    * @Parameters: [employee]
    * @Return void
    */    
    private void initEmployee(Employee employee) {
        // 获取盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 加盐加密
        String saltPassword = MD5Utils.encrypByMd5(salt + employee.getPassword());
        employee.setSalt(salt);
        employee.setPassword(saltPassword);
    }

    /**
    * @Title: update
    * @Description: 重新修改方法
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/6 23:30
    * @Parameters: [employee]
    * @Return void
    */
    @Override
    @Transactional
    public void update(Employee employee) {
        Logininfo logininfo = logininfoMapper.loadById(employeeMapper.loadById(employee.getId()).getLogininfoId());
        Long id = logininfo.getId();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setId(id);
        logininfoMapper.update(logininfo);
        employeeMapper.update(employee);

        // 修改角色关联表数据
        updateRelationTable(employee);

    }

    /**
    * @Title: updateRelationTable
    * @Description: 修改角色关联表数据
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/13 15:54
    * @Parameters: [employee]
    * @Return void
    */
    private void updateRelationTable(Employee employee) {
        if (employee.getRoles() != null){
            // 由于我们不知道用户新增的是否是以前就有的，还要去查询，
            // 那么我们最好的方案是把角色对应的全部删除，在新增
            // 删除员工对应的角色关联表的所有数据
            employeeMapper.removeEmployeeRole(employee.getId());
            // 新增员工对应的角色关联表的所有数据
            employeeMapper.saveEmployeeRole(employee.getId(),employee.getRoles());
        }


    }

    /** 
    * @Title: remove
    * @Description: 从写删除方法
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/6 23:26  
    * @Parameters: [id]
    * @Return void
    */
    @Override
    @Transactional
    public void remove(Long id) {
        // 删除角色关联表数据
        removeRelationTable(id);
        // 同步删除数据
        logininfoMapper.remove(employeeMapper.loadById(id).getLogininfoId());
        employeeMapper.remove(id);

    }

    /**
    * @Title: removeRelationTable
    * @Description: 删除角色关联表数据
    * @Author: Mr.pc
    * @Version: 1.0
    * @Date:  2022/7/13 15:55
    * @Parameters: [id]
    * @Return void
    */
    private void removeRelationTable(Long id) {
        // 删除员工对应的角色关联表的所有数据
        employeeMapper.removeEmployeeRole(id);

    }
}
